﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;

namespace IOTestMain
{
    [Serializable]
    public abstract class testT<t>
    {

    }

    public class ReflectionClass
    {
        public void main()
        {
            Type t = typeof(testT<int>);
            Type t1 = typeof(testT<double>);

            Console.WriteLine(t.Name);
            Console.WriteLine(t1.FullName);
        }
    }

    public static class ObjectCopier
    {
        /// <summary>
        /// Perform a deep Copy of the object.
        /// </summary>
        /// <typeparam name="T">The type of object being copied.</typeparam>
        /// <param name="source">The object instance to copy.</param>
        /// <returns>The copied object.</returns>
        public static T Clone<T>(T source)
        {
            if (!typeof(T).IsSerializable)
            {
                throw new ArgumentException("The type must be serializable.", "source");
            }

            // Don't serialize a null object, simply return the default for that object
            if (Object.ReferenceEquals(source, null))
            {
                return default(T);
            }

            IFormatter formatter = new BinaryFormatter();
            Stream stream = new MemoryStream();
            using (stream)
            {
                formatter.Serialize(stream, source);
                stream.Seek(0, SeekOrigin.Begin);
                return (T)formatter.Deserialize(stream);
            }
        }
    }

    //[Serializable]
    public class protocol
    {
        public int number;
        public string protocolName;

        public protocol(int number, string protocolName)
        {
            this.number = number;
            this.protocolName = protocolName;
        }

        public int Number
        {
            get;
            set;
        }
    }

    [Serializable]
    public class Port
    {
        public string ObjectName;
        public protocol p;

        public Port(string ObjectName, protocol p)
        {
            this.ObjectName = ObjectName;
            this.p = p;
        }

    }

    // Create some custom attributes:
    [AttributeUsage(System.AttributeTargets.Class, Inherited = false)]
    class A1 : System.Attribute { }

    [AttributeUsage(System.AttributeTargets.Class)]
    class A2 : System.Attribute { }

    [AttributeUsage(System.AttributeTargets.Class, AllowMultiple = true)]
    class A3 : System.Attribute { }

    // Apply custom attributes to classes:
    [A1, A2]
    class BaseClass { }

    [A3, A3]
    class DerivedClass : BaseClass { }


    public class GetPropertiesTest
    {
        public static void main()
        {
            protocol p = new protocol(80, "tcp");
            Type t = p.GetType();
            t.GetNestedTypes();
            t.GetCustomAttributes();
            foreach (PropertyInfo property in t.GetProperties())
            {
                Console.WriteLine(property.Name);
            }
        }
    }

}
